<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
        }

        #app {
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
        }

        .title {
            height: 50px;
            width: 100%;
            line-height: 50px;
            /* background-color: antiquewhite; */
            display: flex;
            justify-content: space-between;
        }

        h2 {
            width: 300px;
            height: 50px;

        }

        .clear {
            width: 100px;
            height: 50px;

        }

        .list {
            flex: 1;
        }

        .item {
            margin-top: 20px;
            width: 100%;
            height: 100px;
            display: flex;
        }

        .left {
            /* margin-top: 50px; */
            width: 100px;
            height: 100px;
            margin-left: 20px;
        }

        img {
            width: 100px;
            height: 100px;
            display: block;

        }

        .center {
            flex: 1;
            height: 100%;
            /* background-color: aqua; */
            margin-left: 20px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .right {
            margin-left: 40px;
            width: 400px;
            height: 100%;
            display: flex;
        }

        .jian {
            width: 50px;
            height: 50px;
            margin-top: 30px;
            border-radius: 50px;
            background-color: paleturquoise;
            color: #ccc;
            text-align: center;
            line-height: 50px;
            font-size: 30px;
        }

        .jia {
            width: 50px;
            height: 50px;
            margin-top: 30px;
            border-radius: 50px;
            background-color: #ccc;
            color: paleturquoise;
            text-align: center;
            line-height: 50px;
            font-size: 30px;
        }

        .num {
            margin-left: 20px;
            margin-right: 20px;
            margin-top: 40px;

        }

        .one {
            height: 30px;
            line-height: 30px;
            font-size: 20px;
            font-weight: bold;
        }

        .two {
            height: 30px;
            width: 100%;
        }

        p {
            display: inline-block;
            font-size: 20px;
            color: orangered;
        }

        span {
            display: inline-block;
            color: palegoldenrod;
        }

        .total {
            height: 40px;
            font-size: 20px;
            line-height: 40px;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="title">
            <h2>已选商品</h2><span class="clear" @click="clear()">清空</span>
        </div>
        <div class="list">
            <div class="item" v-for="(item,index) in tasklist" v-bind:key="index">
                <div class="left">
                    <!-- <img src="./img/01.jpg" alt=""> -->
                    <img :src="item.pic" alt="">
                </div>
                <div class="center">
                    <div class="one">{{item.name}}</div>
                    <div class="two">
                        <p>￥{{item.pri}}.00</p>
                        <span class="money">{{item.mon}}</span>
                    </div>
                </div>
                <div class="right">
                    <span class="jian" @click="del(item)">-</span>
                    <span class="num">{{item.num}}</span>
                    <span class="jia" @click="add(item) ">+</span>
                </div>
            </div>
        </div>
        <div class="total">
            总价 <p>{{sum()}}</p>
        </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v = new Vue({
        el: '#app',
        data: {
            tasklist: [
                { id: 1, pic: './img/01.jpg', name: '西红柿/斤', pri: '35', mon: '45', num: 1 }, { id: 2, pic: './img/02.jpg', name: '橘子/斤', pri: '13', mon: '14', num: 1 }, { id: 1, pic: './img/03.jpg', name: '西瓜/个', pri: '40', mon: '65', num: 1 }
            ]
        },
        methods: {

            add(item) {
                if (item.num <= 4) {
                    console.log(item);
                    item.num++
                }
            },
            del(item) {
                if (item.num >= 2) {
                    console.log(item);
                    item.num--
                }
            },
            sum() {
                var price = 0
                this.tasklist.forEach((item) => {
                    
                    console.log(item.num);
                    console.log(item.pri);
                    price += item.num * item.pri
                    // console.log(price);
                })
                return price
            },
            clear() {
                this.tasklist = []
            },
        }
    })
</script>